package com.daasuu.easinginterpolator.slice;

import com.daasuu.easinginterpolator.ResourceTable;
import com.daasuu.ei.Ease;
import com.daasuu.ei.EasingInterpolator;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private DirectionalLayout flowLayout;
    private Button mshowPicture;

    HiLogLabel label = new HiLogLabel(HiLog.DEBUG, 1, "TAG_MainAbilitySlice");

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        flowLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_layout_flow);
        mshowPicture = (Button) findComponentById(ResourceTable.Id_showPic);
        mshowPicture.setClickedListener(this);
        initGraphic();
    }

    private void initGraphic() {
//        EasingGraphView view = new EasingGraphView(this, ease[i]);
////        ComponentContainer.LayoutConfig layoutConfig = (ComponentContainer.LayoutConfig) view.getLayoutConfig();
////        layoutConfig.height = 500;
////        layoutConfig.width = ComponentContainer.LayoutConfig.MATCH_PARENT;
////        flowLayout.addComponent(view);
    }

    @Override
    public void onActive() {
        super.onActive();
        test();

//        Ease[] values = Ease.values();
//        HiLog.debug(label,"aaaa" + values.length);
//        for (int i = 0; i < values.length; i++) {
//            position = new EasingInterpolator(values[i]).getInterpolation(10);
//            System.out.println("position=" + position);
//            Image image = new Image(getContext());
//            HiLog.debug(label,"aaaa i:" + i);
//            image.setPixelMap(ResourceTable.Media_icon);
//            image.setMarginLeft(10);
//            Text text = new Text(getContext());
//            text.setText(values[i].name());
//            DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
//            flowLayout.addComponent(text);
//            flowLayout.addComponent(image, layoutConfig);
//            AnimatorValue animatorValue = new AnimatorValue();
//            animatorValue.setDelay(100);
//            animatorValue.setDuration(5000);
//            animatorValue.setLoopedCount(Animator.INFINITE);
//            animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
//                @Override
//                public void onUpdate(AnimatorValue animatorValue, float v) {
//                    HiLog.debug(label,"bbbbb=" + v);
////                    position += v;
//                    image.setContentPosition((int) position*1000*v, image.getContentPositionY());
//                }
//            });
//            animatorValue.start();
//        }
    }

    private void test() {


        Ease[] values = Ease.values();
        for (int i = 0; i < values.length; i++) {
            EasingInterpolator interpolator = new EasingInterpolator(values[i]);
            Image image = new Image(getContext());
            HiLog.debug(label, "aaaa i:" + 0);
            image.setPixelMap(ResourceTable.Media_icon);
            image.setMarginLeft(10);
            image.setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT);
            image.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
            Text text = new Text(getContext());
            text.setText(values[i].name());
            text.setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT);
            text.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
            flowLayout.addComponent(text);
            flowLayout.addComponent(image);
            AnimatorValue animatorValue = new AnimatorValue();
            animatorValue.setDuration(5000);
            animatorValue.setLoopedCount(Animator.INFINITE);
            animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
                @Override
                public void onUpdate(AnimatorValue animatorValue, float v) {
                    HiLog.debug(label, "bbbbb=" + v);
                    image.setContentPosition((int) 500 * interpolator.getInterpolation(v), image.getContentPositionY());
                }
            });
            animatorValue.start();
        }
    }

//    private void animationTest() {
//        DirectionalLayout flow = (DirectionalLayout)findComponentById(ResourceTable.Id_animation_view);
//
//        for (int i=0; i<4; i++) {
//            Text txt = new Text(getContext());
//            txt.setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT);
//            txt.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
//            txt.setTextSize(30, Text.TextSizeType.FP);
//            txt.setText("Text " +i);
//            flow.addComponent(txt);
//
//            Image image = new Image(getContext());
//            image.setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT);
//            image.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
//            image.setMarginTop(30);
//            image.setPixelMap(ResourceTable.Media_icon);
//            flow.addComponent(image);
//
//            AnimatorValue animator = new AnimatorValue();
//            animator.setDelay(500);
//            animator.setDuration(2000);
//            animator.setLoopedCount(Animator.INFINITE);
//            //animator.setCurveType(Animator.CurveType.CUSTOMIZED);
//
//            animator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
//                @Override
//                public void onUpdate(AnimatorValue animatorValue, float v) {
//                    Random r = new Random();
//                    int step = (r.nextInt(10))*200;
//                    image.setContentPosition((int)(v*step), image.getContentPositionY());
//                }
//            });
//            animator.start();
//
//        }
//
//    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.daasuu.easinginterpolator")
                .withAbilityName("com.daasuu.easinginterpolator.CanvasAbility")
                .build();
        intent.setOperation(operation);
        startAbility(intent);

    }
}
